package org.boticelli.model;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.junit.Before;
import org.junit.Test;

public class LogEntryTestCase
{
    private Calendar cal;
    
    @Before
    public void init()
    {
        cal=Calendar.getInstance();
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        
    }

    @Test
    public void thatDayIsCalculatedCorrectly()
    {
        LogEntry log=new LogEntry();
        cal.set(Calendar.YEAR , 1970);
        cal.set(Calendar.MONTH , 0);
        cal.set(Calendar.DATE , 2);
        log.setMoment(new Timestamp(cal.getTime().getTime()));        
        assertThat(log.getDay(), is(1));
        
        cal.set(Calendar.YEAR , 1971);
        cal.set(Calendar.MONTH , 0);
        cal.set(Calendar.DATE , 1);
        log.setMoment(new Timestamp(cal.getTime().getTime()));
        
        assertThat(log.getDay(), is(365));

    }
    
    @Test
    public void thatTheDayChangesAtNoon()
    {
        LogEntry log=new LogEntry();
        LogEntry log2=new LogEntry();
        
        Calendar cal2=(Calendar)cal.clone();
        
        cal2.add(Calendar.SECOND, -1);
        log.setMoment(new Timestamp(cal.getTime().getTime()));
        log2.setMoment(new Timestamp(cal2.getTime().getTime()));
        
        DateFormat format=SimpleDateFormat.getDateTimeInstance();
        
        System.out.println(format.format(cal.getTime()));
        System.out.println(format.format(cal2.getTime()));

        assertThat(log.getDay(), is(log2.getDay()+1));
    }
}
